<template>
  <div>
    <div class="header">
      <vy-theme-header :tag="6" :projectid="projectId"></vy-theme-header>
    </div>
    <div class="page-list">
      <mu-flex
        class="main-operate"
        align-items="center"
        justify-content="between"
      >
        <mu-flex align-items="center">
          <mu-select
            class="search-select"
            solo
            popover-class="select-menu"
            v-model="select"
            @change="groupSelect"
          >
            <mu-option label="全部" value="0"></mu-option>
            <mu-option
              v-for="item in groups"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></mu-option>
          </mu-select>
          <span class="content-count">
            共<span class="num">{{ dataCount }}</span
            >条内容
          </span>
        </mu-flex>
        <mu-flex align-items="center">
          <mu-paper :z-depth="1" class="search-input">
            <a-input
              v-model="searchContent"
              placeholder="请输入关键词搜索"
              :allowClear="true"
              @pressEnter="onSearch"
            >
              <a-icon slot="suffix" type="search" @click="onSearch" />
            </a-input>
          </mu-paper>
          <div>
            <span class="sort-title">排序：</span>
            <mu-select
              class="sort-select"
              solo
              popover-class="select-menu"
              v-model="order"
              @change="orderSelect"
            >
              <mu-option label="最新发布" value="0"></mu-option>
              <mu-option label="点赞最多" value="1"></mu-option>
              <mu-option label="评论最多" value="2"></mu-option>
            </mu-select>
          </div>
        </mu-flex>
      </mu-flex>
      <mu-load-more :loaded-all="loadAll" :loading="loading" @load="moreData">
        <div v-if="list && list.length > 0">
          <mu-flex id="data_container" class="main-list">
            <div v-for="item in list" :key="item.id" class="list-item">
              <mu-paper class="item-main" @click="redirectToDetail(item.id)">
                <img
                  v-if="item.type === 2"
                  class="item-img-type"
                  src="https://image.xuexiwangzhe.com/FiiWsL8JHuI9Xk3_PJwsgkdQEGia"
                />
                <img
                  v-if="item.type === 1"
                  class="item-img-type"
                  src="https://image.xuexiwangzhe.com/Fpqer2GLjZTEBUz_fHUVEuzZqAI1"
                />
                <img
                  :src="
                    item.type === 1 ? baseImgUrl + item.cover : item.ossImgUrl
                  "
                  class="item-img"
                />
                <div class="item-content">
                  <div class="item-info">
                    {{ item.content }}
                  </div>
                  <mu-flex align-items="center" justify-content="between">
                    <mu-flex align-items="center">
                      <mu-avatar class="item-avatar">
                        <img :src="baseImgUrl + item.userAvatar" />
                      </mu-avatar>
                      <span>{{ item.nickName }}</span>
                    </mu-flex>
                    <mu-flex class="item-zan" align-items="end">
                      <img
                        v-show="item.isPraise === 0"
                        src="https://image.xuexiwangzhe.com/FkZWjt1J9AccrGbHaVKYSr8hBbc9"
                        class="zan-img"
                        @click.stop="praise(item)"
                      />
                      <img
                        v-show="item.isPraise === 1"
                        src="https://image.xuexiwangzhe.com/Fr0nnsllSEtfzDfoeNTesqHTjwTF"
                        class="zan-img"
                        @click.stop="praise(item)"
                      />
                      <span>{{ item.supportNum }}</span>
                    </mu-flex>
                  </mu-flex>
                </div>
              </mu-paper>
            </div>
          </mu-flex>
          <div class="more" v-show="!more && !loading && !loadAll">
            <a @click="moreClick">查看更多</a>
          </div>
          <!-- <div class="complete" v-show="!loading && loadAll">
            已加载全部数据
          </div> -->
        </div>
        <div class="complete" v-show="(!list || list.length <= 0) && !loading">
          暂无数据
        </div>
      </mu-load-more>
    </div>
  </div>
</template>
<script>
import Macy from "macy";
import VyHeader from "../../../components/theme-header.vue";
export default {
  data() {
    return {
      projectId: "",
      select: "0",
      order: "0",
      searchContent: "",
      search: "",
      list: [],
      groups: [],
      page: 1,
      size: 10,
      dataCount: 0,
      loading: false,
      more: false,
      loadAll: false,
      baseImgUrl: "https://image.xuexiwangzhe.com/",
    };
  },
  components: {
    [VyHeader.name]: VyHeader,
  },
  created() {
    this.projectId = this.$route.query.projectId;
    this.getData();
    this.getGroupList();
  },
  mounted() {},
  methods: {
    resetPage() {
      this.loadAll = false;
      this.more = false;
      this.page = 1;
      this.list = [];
    },
    onSearch() {
      this.search = this.searchContent;
      this.resetPage();
      this.getData();
    },
    groupSelect() {
      this.resetPage();
      this.getData();
    },
    orderSelect() {
      this.resetPage();
      this.getData();
    },
    praise(item) {
      const params = {
        itemType: 14,
        id: item.id,
        projectId: this.projectId,
        moduleId: 0,
        SESSIONKEY: sessionStorage.getItem("SESSIONKEY"),
      };
      this.$axios.post("/praiselog/praise", params).then((res) => {
        if (res.success) {
          if (item.isPraise === 0) {
            item.isPraise = 1;
            item.supportNum += 1;
          } else {
            item.isPraise = 0;
            item.supportNum -= 1;
          }
        }
      });
    },
    redirectToDetail(id) {
      const _queryPms = new URLSearchParams(this.$route.query);
      this.$router.push("/detail/" + id + "?" + _queryPms);
    },
    getGroupList() {
      const params = {
        projectId: this.projectId,
      };
      this.$axios.get("/userGroup/list", { params }).then((res) => {
        if (res.success) {
          this.groups = res.userGroups;
        }
      });
    },
    getData() {
      this.loading = true;
      const params = {
        projectId: this.projectId,
        groupId: this.select,
        search: this.search,
        order: this.order,
        page: this.page,
        size: this.size,
      };
      this.$axios.get("/momentcontent/list", { params }).then((res) => {
        if (res.success) {
          const result = res.momentContentVoPage;
          if (result) {
            if (result.last) this.loadAll = true;
            this.list = this.list.concat(result.content);
            this.dataCount = result.totalElements;
          } else {
            this.list = [];
            this.dataCount = 0;
            this.loadAll = true;
          }

          this.$nextTick(function() {
            Macy({
              container: "#data_container",
              trueOrder: false,
              waitForImages: true,
              margin: {
                x: 0,
                y: 30,
              },
              columns: 5,
            });
            this.loading = false;
          });
        }
        this.loading = false;
      });
    },
    moreClick() {
      this.more = true;
      this.page += 1;
      this.getData();
    },
    moreData() {
      if (!this.more) return;
      this.page += 1;
      this.getData();
    },
  },
};
</script>

<style lang="less">
.page-list {
  width: 1100px;
  margin: 0 auto;
  height: 100%;
  .main-operate {
    padding-top: 50px;
    margin-bottom: 20px;
  }

  .content-count {
    color: #a3a3a3;
    font-size: 16px;
    .num {
      color: #5a6e96;
      font-size: 22px;
      padding-left: 5px;
      padding-right: 5px;
    }
  }

  .sort-title {
    color: #a3a3a3;
    font-size: 16px;
  }

  .sort-select {
    width: 100px;
  }

  .main-list {
    margin-left: -15px;
    margin-right: -15px;
    flex-wrap: wrap;
  }

  .more,
  .complete {
    margin-bottom: 30px;
    text-align: center;
  }

  .complete {
    color: #666;
    padding-top: 80px;
  }

  .list-item {
    width: 270px;
    padding-left: 15px;
    padding-right: 15px;
    margin-bottom: 20px;
  }

  .item-main {
    cursor: pointer;
    position: relative;
    border-radius: 12px;
    box-shadow: 2px 5px 7px 0px rgba(197, 197, 197, 0.2);
    transition: box-shadow 0.1s;
    &:hover {
      box-shadow: 5px 8px 10px 3px rgba(14, 23, 38, 0.1);
    }
  }

  .item-img {
    width: 100%;
    border-top-left-radius: 12px;
    border-top-right-radius: 12px;
  }

  .item-img-type {
    position: absolute;
    top: 10px;
    right: 10px;
    width: 30px;
  }

  .item-content {
    padding: 12px 15px 14px 17px;
    line-height: 1;
  }

  .item-info {
    color: #4f4f4f;
    margin-bottom: 20px;
    line-height: 20px;
  }

  .item-avatar {
    margin-right: 10px;
  }

  .item-zan {
    color: #808080;
  }

  .zan-img {
    cursor: pointer;
    width: 20px;
    margin-right: 10px;
  }

  .search-select {
    border-radius: 8px;
    background-color: #3366ff;
    width: 100px;
    height: 45px;
    min-height: auto;
    margin-right: 20px;
    input {
      color: white;
      text-align: center;
    }
    .mu-select-action {
      color: white;
      margin-left: -20px;
    }
  }

  .search-input {
    width: 230px;
    border-radius: 20px;
    box-shadow: 0px 2px 4px 0px rgba(0, 0, 0, 0.14);
    margin-right: 25px;
    input {
      &::placeholder {
        color: #a3a3a3;
      }
      border: none;
      border-radius: 20px;
      height: 38px;
      padding-left: 15px;
    }
  }
}
</style>
